<template>
  <div>
    <el-form :model="queryParams" ref="queryForm" size="mini" v-show="showSearch" label-width="68px">
      <el-row :gutter="20">
        <el-col :span="4">
          <el-form-item label="姓名" prop="staffName">
            <el-input v-model="queryParams.staffName" placeholder="请输入" clearable @keyup.enter.native="handleQuery" />
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="职别" prop="staffLevels">
            <el-select v-model="queryParams.staffLevels" placeholder="请选择" clearable>
              <el-option v-for="item in categoryOptions" :key="item.jobCategoryId" :label="item.jobName"
                :value="item.jobCategoryId" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="离职时间">
            <el-date-picker v-model="dateRange" value-format="yyyy-MM-dd" type="daterange" range-separator="-"
              start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label-width="10px">
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-table v-loading="loading" :data="staffList" border>
      <el-table-column label="员工编号" align="center" prop="staffId" width="80px;"/>
      <el-table-column label="员工姓名" align="center" prop="staffName" width="100px;"/>
      <el-table-column label="性别" align="center" prop="staffSex" width="80px;">
        <template slot-scope="scope">
          {{ formatGender(scope.row.staffSex) }}
        </template>
      </el-table-column>
      <el-table-column label="年龄" align="center" width="80px;">
        <template slot-scope="scope">
          {{ calculateAge(scope.row.staffIdcard) }}
        </template>
      </el-table-column>
      <el-table-column label="身份证号" align="center" prop="staffIdcard" width="200px;"/>
      <el-table-column label="联系方式" align="center" prop="staffPhone" width="120px;"/>
      <!--<el-table-column label="职别" align="center" prop="jobName" width="100px;"/>-->
      <el-table-column label="银行卡号" align="center" prop="bankCard" width="200px;">
        <template slot-scope="scope">
          {{ scope.row.bankCard ? scope.row.bankCard.replace(/\s/g, '').replace(/[^\d]/g, '').replace(/(\d{4})(?=\d)/g, '$1 ') : '' }}
        </template>
      </el-table-column>
      <el-table-column label="保险单号" align="center" prop="insuranceOdd" width="200px;"/>
      <el-table-column label="紧急联系人" align="center" prop="urgentLinkman" width="100px;"/>
      <el-table-column label="紧急联系人电话" align="center" prop="urgentLinkmanPhone" width="120px;"/>
      <el-table-column label="家庭住址" align="center" prop="staffAddress"/>
      <el-table-column label="离职原因" align="center" prop="resignReason"/>
      <el-table-column label="离职申请表" align="center" prop="resignTableImg"width="100px;">
        <template slot-scope="scope">
          <el-button v-if="scope.row.resignTableImg" type="primary" size="mini" @click="handleDownload(scope.row)">查看</el-button>
        </template>
      </el-table-column>
      <!--<el-table-column label="推荐人" align="center" prop="recommenStaffId" width="100px;"/>-->
      <el-table-column label="离职时间" align="center" prop="resignTime" width="100px;">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.resignTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="200px;">
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="handleReinstatement(scope.row)"
            v-hasPermi="['cld:staff:reinstatement']">恢复入职</el-button>
          <el-button size="mini" type="text" @click="handleResume(scope.row)"
            v-hasPermi="['cld:staff:resume']">履历表</el-button>
          <el-button size="mini" type="text" @click="handleSetBlacklist(scope.row)"
            v-hasPermi="['cld:staff:blacklist']">设置黑名单</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

     <!-- 履历表查看 -->
     <el-dialog :close-on-click-modal="false" :destroy-on-close="true" :visible.sync="resumeOpen" append-to-body title="履历表"
               width="900px">
               <div id="printLLBForm">
        <staff-resume-view :staffId="staffId"> </staff-resume-view>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button v-print="'#printLLBForm'" type="primary"
          >打印</el-button
        >
        <el-button @click="resumeOpen = false">关 闭</el-button>
    </div>
    </el-dialog>
  </div>
</template>

<script>
import { listStaff, getStaff, delStaff, addStaff, updateStaff,reinstatementStaff } from "@/api/cld/staff";
import { getListCategory } from '@/api/cld/category'
import staffResumeView from './staffResumeView.vue';
export default {
  name: "Staff",
  dicts: ['cld_staff_levels'],
  inject: ['formatGender'],
  components: {
    staffResumeView
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 人员花名册表格数据
      staffList: [],
      // 弹出层标题
      title: "",
      resumeOpen: false,
      resumeImg: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        staffName: null,
        staffLevels: null,
        staffStatus: 3,
        isPrepared: 0
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      // 职别列表
      categoryOptions: [],
      dateRange: [],
      staffId: null
    };
  },
  created() {
    this.getList();
    // 获取职别列表
    this.handleGetListCategory();
  },
  methods: {
    /** 查询人员花名册列表 */
    getList() {
      this.loading = true;
      listStaff(this.addDateRange(this.queryParams, this.dateRange, 'ResignTime')).then(response => {
        this.staffList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 职别列表
    handleGetListCategory() {
      getListCategory().then(res => {
        this.categoryOptions = res.data;
      })
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 设置黑名单
    handleSetBlacklist(row) {
      this.$prompt('', '拉黑原因', {
        center: true,
        inputType: 'textarea',
        confirmButtonText: '确定',
        cancelButtonText: '取消',
      }).then(({value}) => {
        row.staffStatus = 4;
        row.blackReason = value;
        updateStaff(row).then(response => {
          this.getList();
          this.$modal.msgSuccess("操作成功");
        });
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal.confirm('是否确认删除员工姓名为"' + row.staffName + '"的数据项？').then(function () {
        return delStaff(row.staffId);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    // 查看离职申请表
    handleDownload(row) {
      const link = document.createElement('a');
      link.href = row.resignTableImg;
      link.setAttribute('download', '离职申请表-' + row.staffName);
      link.click();
    },
     // 履历表查看
     handleResume(row){
      this.resumeOpen = true;
      this.staffId = row.staffId;
    },
     // 恢复已入职
     handleReinstatement(row) {
      this.$modal.confirm('是否将员工姓名为"' + row.staffName + '"恢复为已入职？').then(() => {
        reinstatementStaff(row.staffId).then(res => {
          this.getList();
          this.$modal.msgSuccess("操作成功");
        });

      }).catch(() => { });
    },
  }
};
</script>
<style scoped>
.el-range-editor.el-input__inner {
  width: 100%;
}

.el-select {
  width: 100%;
}
.el-image{
  width: 100%;
}
</style>
